﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using GameLayer;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GameInterface;
using Microsoft.Xna.Framework.Content;
using GameCharacter;
namespace GameInstrument
{
    public class Fire : Button, ICollision<Sprite>, ICollision<Character>
    {
        public static bool Initialized = false;
        public static Sprite type0 { get; set; }

        public static Sprite type1 { get; set; }

        public static Sprite type2 { get; set; }

        public static Sprite type3 { get; set; }

        public static Sprite type4 { get; set; }

        public static void LoadContent(ContentManager content)
        {
            if (!Initialized)
            {
                type0 = new Sprite(content, "Images/Sprites/Effects/Fires/Type0/", 6);
                type1 = new Sprite(content, "Images/Sprites/Effects/Fires/Type1/", 6);
                type2 = new Sprite(content, "Images/Sprites/Effects/Fires/Type2/", 6);
                type3 = new Sprite(content, "Images/Sprites/Effects/Fires/Type3/", 6);
                type4 = new Sprite(content, "Images/Sprites/Effects/Fires/Type4/", 6);
            }
            Initialized = true;
        }
        public static void UnloadContent()
        {
            if (Initialized)
            {
                type0.Dispose();
                type1.Dispose();
                type2.Dispose();
                type3.Dispose();
                type4.Dispose();
            }
            Initialized = false;
        }
        public Fire(ContentManager content)
        {
            if (!Initialized)
            {
                LoadContent(content);
            }
            this.Items.AddRange(new Sprite[] { type0.Clone() as Sprite, type1.Clone() as Sprite, type2.Clone() as Sprite, type3.Clone() as Sprite, type4.Clone() as Sprite });
        }
        public Fire(List<Sprite> items)
        {
            this.Items = items;
        }

        protected override void UpdateEntitys(GameTime gameTime, object extraData)
        {
            var currentSprite = this.Items[this.Index];
            if (currentSprite.Index != currentSprite.nTextures - 1)
            {
                this.Items[Index].Update(gameTime);
                this.Items[Index].NextFrame();
            }
            else
            {
                this.Visible = false;
                this.Enable = false;
            }
            base.UpdateEntitys(gameTime, extraData);
        }

        public bool CollisionWith(Sprite other, bool pixelLevel)
        {
            return this.Items[Index].CollisionWith(other, pixelLevel);
        }

        public override object Clone()
        {
            var items = this.Items.Select(x => (Sprite)x.Clone()).ToList<Sprite>();
            Fire newFire = new Fire(items);
            return newFire;
        }

        public void SetType(int index)
        {
            this.Index = index;
        }

        public bool CollisionWith(Character other, bool pixelLevel)
        {
            return other.CollisionWith(this.Items[Index], pixelLevel);
        }
    }
}
